package afk.biglog.gui;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;

import afk.biglog.*;

public class FileViewAdapter implements BlockListener, Runnable, ActionListener
{

	private static final String B_FILTER = "B_FILTER";
	private final File file;
	private final BlockFilter filter;

	public File getFile()
	{
		return file;
	}

	public BlockFilter getFilter()
	{
		return filter;
	}

	public JButton getFilterButton()
	{
		return filterButton;
	}

	public JTextArea getFilterText()
	{
		return filterText;
	}

	public JPanel getMainPanel()
	{
		return mainPanel;
	}

	private final JButton filterButton;
	private final JTextArea filterText;
	private final JPanel mainPanel;
	private Thread thread;
	private final FileBlockIndexer blockIndexer;

	private final BlockReader blockReader;
	private final ErrorHandler errorHandler;
	private final JTable mainTable;
	private final BlockTableModel dm;

	public FileViewAdapter(final File file, final String encoding, final int offset, final int linesPerBlock, final ErrorHandler handler) throws FileNotFoundException
	{
		errorHandler = handler;
		filterText = new JTextArea();
		mainPanel = new JPanel(new BorderLayout());
		this.file = file;
		filter = new BlockFilter();
		blockIndexer = new FileBlockIndexer(file, linesPerBlock, offset, errorHandler);
		filterButton = createFilterButton();
		blockReader = new BlockReader(file, encoding);

		dm = new BlockTableModel(blockReader, errorHandler, filter);
		mainTable = new JTable(dm);
		mainTable.setDefaultRenderer(String.class, new BlockCellRenderer());
		mainTable.setFont(new Font("courier new", Font.PLAIN, 12));
		final String name = file.getAbsolutePath();
		final JScrollPane jScrollPane = createScrollPane(name, mainTable);

		blockIndexer.setFinish(true);
		blockIndexer.addBlockListener(this);

		final Box box = new Box(BoxLayout.X_AXIS);
		box.add(filterText);
		box.add(filterButton);

		mainPanel.add(box, BorderLayout.NORTH);
		mainPanel.add(jScrollPane, BorderLayout.CENTER);
	}

	private JScrollPane createScrollPane(final String name, final JComponent collectionPanel)
	{
		final JScrollPane jScrollPane = new JScrollPane(collectionPanel);
		jScrollPane.setToolTipText(name);
		return jScrollPane;
	}

	private JButton createFilterButton()
	{
		final JButton filterButton = new JButton("Filter");
		filterButton.setDefaultCapable(true);
		filterButton.addActionListener(this);
		filterButton.setActionCommand(B_FILTER);
		return filterButton;
	}

	public void startIndex()
	{
		thread = new Thread(this, file.getName() + " Indexer");
		thread.start();
	}

	@Override
	public void newBlock(final Block block)
	{
		dm.addBlock(block);
	}

	@Override
	public void run()
	{
		try
		{
			System.out.println("Starting index");
			blockIndexer.index();
			System.out.println("Finished index");
		}
		catch (final Exception e)
		{
			errorHandler.addError("While indexing " + file + ": ", e);
		}
	}

	@Override
	public void actionPerformed(final ActionEvent e)
	{
		if (e != null)
			if (e.getActionCommand() == B_FILTER)
			{
				updateFilter();
				cleanTextArea();
			}
	}

	private void updateFilter()
	{
		filter.getWhiteList().clear();
		if (filterText.getText().length() > 0)
			filter.getWhiteList().add(filterText.getText());
	}

	private void cleanTextArea()
	{
		mainTable.invalidate();
	}

	public void close()
	{
		try
		{
			blockReader.close();
		}
		catch (final IOException e)
		{
			errorHandler.addError("While closing File " + file + ": ", e);
		}
		blockIndexer.abort();
		thread.interrupt();
	}

}
